package ytdp.careagle.com.ytdp.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.mvp.mode.LoginModel;
import ytdp.careagle.com.ytdp.mvp.view.ILogoutView;
import ytdp.careagle.com.ytdp.net.MyObserver;
import ytdp.careagle.com.ytdp.net.Result;

/**
 * Created by admin on 2017/10/12.
 */

public class LogoutPresenter {
    private ILogoutView view;
    private LoginModel model;

    public LogoutPresenter(ILogoutView view) {
        this.view = view;
        model = new LoginModel();
    }

    public void logout(Context context) {
        String password = view.getPassword();
        if (TextUtils.isEmpty(password)) {
            view.showMessage("密码不能为空");
            return;
        }
        view.showProgress("正在退出");
        model.goLogout(context, password, new MyObserver<Result>() {
            @Override
            public void onNext(Result result) {
                super.onNext(result);
                if (result.getCode() == G.REQUEST_OK) {
                    view.logoutSuccess();
                } else {
                    view.showMessage(result.getMessage());
                }
                view.hideProgress();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.showMessage("服务器错误");
                view.hideProgress();
            }
        });
    }
}
